<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <div>
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/No1_logo.png" />
              <div class="title">原始人宿舍</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoute" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
    </a-col>
    <a-col flex="40px">
      <div>
        <img :src="avatar" />
        {{ store.state.user?.loginUser?.name ?? "未登录" }}
      </div>
    </a-col>
    <a-button @click="exit" type="text" status="danger">退出</a-button>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import { IconUser } from "@arco-design/web-vue/es/icon";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
const store = useStore();
const avatar = store.state.user.loginUser.avatarurl;
console.log("avatar", avatar);
const visibleRoute = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

// const visibleRoute = routes.filter((item, index) => {
//   if (item.meta?.hideInMenu) {
//     return false;
//   }
//   // todo 根据权限过滤菜单
//   if (!checkAccess(loginUser, item?.meta?.access as string)) {
//     return false;
//   }
//   return true;
// });

// eslint-disable-next-line no-undef
const selectedKeys = ref(["/"]);

router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

const exit = () => {
  router.push("/user/login");
};

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "远山",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 15px;
}

.logo {
  height: 48px;
}
</style>
